package com.wtx.chapter05;

/**
 * @description: leetcode 203
 * @author: wtx
 * @createDate: 2020/5/19
 */
public class Solution {

    public ListNode removeElements(ListNode head, int val) {

        while ( head!=null && head.val == val){
            ListNode delNode = head;
            head = head.next;
            delNode.next = null;
        }

        if( head == null )
            return null;

        //此时head一定不是要删除的节点
        ListNode prev = head;
        while (prev.next!=null){

            if(prev.next.val == val){
                ListNode delNode = prev.next;
                prev.next = delNode.next;
                delNode.next = null;
            }else
                prev = prev.next;
        }

        return head;
    }
}
